<?php
// $Id$

/**
 * Controller_Admin_Leftmenu 控制器
 */
class Controller_Admin_Leftmenu extends Controller_Abstract
{

	function _before_execute()
	{
		//检测是否登陆状态
		$this->_check_Admin_Login();
	}

	function actionIndex()
	{
		// 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		$id = intval($this->_context->id);
		if ($id == 0)
		{
			exit('参数错误');
		}else{
			$leftmenu = helper_menu_level::_get_UserMenu($_SESSION['acct']['acclevel'],$id);
			if (is_array($leftmenu))
			{
				$this->_view['leftmenu'] = $leftmenu;
			}else {
				exit('可能您没有权限访问此页面');
			}

		}

	}
	
	function actiontop()
	{
		
	}
	/**
	 * 主页显示，主要做了显示最近7日内下级流量代理消费总额，和系统对应用户的充值总额
	 */
	function actionbottom()
	{
		$acct_id = $_SESSION['acct']['id'];
	    $total_user = acctuser::find("acct_id = $acct_id")->getCount();
	    $date = strtotime(date("Y-m-d",time()));
	    $today_user = acctuser::find("acct_id = $acct_id and addtime > $date")->getCount();
	    $total_fee = coreorder::find("acct_id = $acct_id and status = 1")->getSum("fee");
	    $today_fee = coreorder::find("acct_id = $acct_id and created > $date and status = 1")->getSum("fee");
	    $res = array(
	    	"total_user"=>$total_user?$total_user:0,
	    	"today_user"=>$today_user?$today_user:0,
	    	"total_fee"=>$total_fee?$total_fee:0,
	    	"today_fee"=>$today_fee?$today_fee:0
	    );
	    $this->_view['info']=$res;
	}
    function actionUpvup()
    {

    }
	function actionleft()
	{
		
	}

	/**
	 * 更新授权
	 */
	function actionauthorize(){
  
	    $time = time();
	    $sign = md5(sprintf('%d%s',$time,'edulive66688!@#*&^')) ;
        $authdir = Q::ini ( 'app_config/ROOT_DIR' ) .'/tmp/auth';
        if(!is_dir($authdir)){
            @mkdir ( $authdir, 0700 );
        }
        $authfile = $authdir.'/auth.txt';
        $authresult = Helper_Common::cget('http://eduauth.huiyaotech.cn/interface/auth?sign='.$sign .'&time='.$time);
      
       if(empty($authresult)){
           exit(json_encode(array('code'=>1,'请求失败服务器更新异常')));
       }else{
           $authresult_array = json_decode($authresult,true);
           if(is_array($authresult_array)){
               if($authresult_array['code'] != 0){
                   exit(json_encode(array('code'=>2,'msg'=>$authresult_array['msg'])));
               }else{
                   //出于安全考虑 授权服务器返回的是3des加密后的字符串--防止被抓包破解数据
                 
                  file_put_contents($authfile, $authresult_array['data']);
                  exit(json_encode(array('code'=>0,'授权更新成功')));
               }
             
           }else{
               exit(json_encode(array('code'=>1,'msg'=>'请求错误')));
           }
           
       }
  
	}
	
	/**
	 * 服务器自动更新
	 */
	function actionupdatetonew(){
	    


	    global $root_dir;
	    
	    $checktag = false;
	    $time = time();
	    
	    $now_version = EDU_VERSION;
	    
	    $sign = md5(sprintf('%d%s%s',$time,'edulive66688!@#*&6677^',$now_version)) ;
	    $authresult = Helper_Common::cget('http://eduauth.huiyaotech.cn/interface/checkupdate?sign='.$sign .'&time='.$time.'&version='.$now_version);
	    //var_dump($authresult);
	    //{"code":0,"data":"wwD9922g2QF+6JjFP3+wt+mNJdGi6I9t","sqldownurl":"http:\/\/eduauth.huiyaotech.cn\/1.sql"}
	    $authresult = json_decode($authresult,true);
	    if(is_array($authresult) && $authresult['code'] == 0){
	        echo '开始更新<br/>';
	        $checktag = true;
	        $sqlurl = $authresult['sqldownurl'];
	         
	    }else{
	        if(empty($authresult)){
	            echo '更新网络请求出错<br/>';
	        }else{
	            if(!empty($authresult['msg'])){
	                echo $authresult['msg'];die;
	            }else{
	                echo '请求出错啦<br/>';die;
	            }
	        }
	    }
	    
	    if ($checktag){
	         
	        //备份数据库
	         
	        $sqlbak_filename = '/tmp/'.EDU_VERSION.'_'.date('YmdHis').'sql.gz';
	         
	        $exec="/usr/bin/mysqldump -hlocalhost -uedulive -proot edulive | gzip > {$sqlbak_filename}";
	        exec($exec,$result,$status);
	    
	        if(is_file($sqlbak_filename) && filesize($sqlbak_filename) > 0){
	            echo "数据库备份成功<br/>";
	        }
	    
	        //备份核心文件
	        echo "开始备份 核心文件<br/>";
	        $app_path =  Q::ini ( 'app_config/ROOT_DIR' ) . '/app';
	        $dest = '/tmp/app'.$now_version;
	        $this->copydir($app_path, $dest);
	        echo "核心文件备份完成<br/>";
	    
	        //备份配置文件
	        echo "开始备份配置文件<br/>";
	        $config_path =  Q::ini ( 'app_config/ROOT_DIR' ) . '/config';
	        $dest = '/tmp/config'.$now_version;
	        $this->copydir($config_path, $dest);
	        echo "配置文件备份完成<br/>";
	         
	        //备份完成开始更新
	    
	        //更新数据库文件---数据库文件至关重要，如果更新失败了不能继续执行下面的操作 防止整套系统宕掉
	        echo "开始更新数据库<br/>";
	        $sqlupdate = file_get_contents($sqlurl);
	    
	    
	        try {
	            QDB::getConn()->execute($sqlupdate);
	            echo '数据库更新成功';
	        } catch (Exception $e) {//数据库如果更新文件报错停止更新
	            echo "<b style= 'color:red'>数据库更新报错,自动更新终止</b><br/>";
	            Helper_Common::log('数据库更新报错'.print_r($e->getMessage())."<br/>");
	            print_r($e->getMessage())."<br/>";
	            die;
	        }

	        //更新配置文件
	        $update_config_sh = 'cd '.$root_dir.'/config/  &&  /usr/bin/svn update --accept tc --force --no-auth-cache --username "devel" --password "devel168178"';
	        exec($update_config_sh,$result,$status);
	         
	        if($status){
	            //更新成功
	            echo "<c style='color:red;'>配置文件更新失败<br/></c>";
	        }else{
	            echo "配置文件更新成功<br>";
	            Helper_Common::log('配置文件命令成功, 结果如下<br>'.  var_export( $result,true)."<br/>");
	            print_r( $result );
	        }
	         
	        //更新public
	         
	        $update_public_sh = '/usr/bin/svn update /data/edulive/public/updateadd --accept tc --force --no-auth-cache --username "devel" --password "devel168178"';
	        // echo $update_public_sh;
	        exec($update_public_sh,$result,$status);
	    
	         
	        if($status){
	            //更新成功
	    
	            echo "<c style='color:red;'>公共样式文件public更新失败<br/></c>";
	            print_r( $result );
	        }else{
	            echo "公共样式文件public更新成功<br>";
	            print_r( $result );
	            Helper_Common::log('公共样式文件public更新成功, 结果如下, 结果如下<br>'.  var_export( $result,true)."<br/>");
	        }
	        //  die;
	    
	        //更新代码app
	        $update_app_sh = 'cd '.$root_dir.'/app/  &&  /usr/bin/svn update --accept tc --force --no-auth-cache --username "devel" --password "devel168178"';
	    
	        exec($update_app_sh,$result,$status);
	        if($status){
	            //更新成功
	            echo "<c style='color:red;'>核心代码app更新失败<br/></c>";
	            print_r( $result );
	        }else{
	            echo "核心代码更新成功<br/>";
	            Helper_Common::log('核心app文件更新成功, 结果如下<br>'.  var_export( $result,true)."<br/>");
	            //print_r( $result );
	        }
	        //删除缓存文件  /tmp/runtime/cache的子文件
	        $this->deldir(Q::ini ( 'app_config/ROOT_DIR' ) . '/tmp/runtime_cache/');
	        echo "缓存文件删除成功，系统自动更新完成。<br/>当前版本为<b style = 'color:red'>".EDU_VERSION."</b>";
	        die;
	    }
	     
	}
	
	

}


